//
//  MainViewController.m
//  Auditory Sense Bus
//
//  Created by 寒竹子 on 15/4/28.
//  Copyright (c) 2015年 寒竹子. All rights reserved.
//

#import "MainViewController.h"
#import "ParentViewController.h"
#import "PicsViewController.h"
#import "XRMusicMainViewController.h"
#import "MeViewController.h"

@interface MainViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UISegmentedControl * segment;
@property (nonatomic, strong) UIScrollView * scrollView;

@property (nonatomic, strong) NSMutableArray * viewControllers;
@property (nonatomic, assign) NSInteger currentIndex;

@end

@implementation MainViewController

- (NSMutableArray *)viewControllers
{
    if (nil == _viewControllers) {
        _viewControllers = [[NSMutableArray alloc] init];
        NSArray * titles = @[@"PicsViewController", @"XRMusicMainViewController", @"MeViewController"];
        for (int i = 0; i < titles.count; i++) {
            Class subCls = NSClassFromString(titles[i]);
            ParentViewController * pVc = [[subCls alloc] init];
            [_viewControllers addObject:pVc];
        }
        
        // 传入导航 用于push
        PicsViewController * picVc = _viewControllers[0];
        picVc.rootNav = (RootNavigationController *)self.navigationController;
        XRMusicMainViewController * musicVc = _viewControllers[1];
        musicVc.rootNav = (RootNavigationController *)self.navigationController;
        MeViewController * meVc = _viewControllers[2];
        meVc.rootNav = (RootNavigationController *)self.navigationController;
    }
    
    return _viewControllers;
}

- (UISegmentedControl *)segment
{
    if (_segment == nil) {
        _segment = [[UISegmentedControl alloc] initWithItems:@[@"魅影", @"听觉", @"我的"]];
        _segment.frame = CGRectMake(0, 0, 200.0f, 44.0f);
        
        NSDictionary * normalDict = @{NSFontAttributeName : TextFont(18.0f), NSForegroundColorAttributeName : RGB(100, 100, 100, 1.0f)};
        [_segment setTitleTextAttributes:normalDict forState:UIControlStateNormal];
        
        NSDictionary * seletedDict = @{NSFontAttributeName : TextFont(20.0f), NSForegroundColorAttributeName : RGB(49, 49, 49, 1.0f)};
        [_segment setTitleTextAttributes:seletedDict forState:UIControlStateSelected];
        _segment.tintColor = [UIColor clearColor];
        [_segment setSelectedSegmentIndex:1];
        _currentIndex = _segment.selectedSegmentIndex;
        
        [_segment addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
    }
    
    return _segment;
}

- (UIScrollView *)scrollView
{
    if (nil == _scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        
        for (int i = 0; i < self.viewControllers.count; i++) {
            ((ParentViewController *)(_viewControllers[i])).view.frame = CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height);
            [_scrollView addSubview:((ParentViewController *)(_viewControllers[i])).view];
        }
        
        _scrollView.contentSize = CGSizeMake(self.view.frame.size.width * self.viewControllers.count, self.view.frame.size.height);
        [_scrollView setContentOffset:CGPointMake(self.view.frame.size.width * _currentIndex, 0) animated:YES];
    }
    
    return _scrollView;
}

- (void)setupUI
{
    self.view.backgroundColor = RGB(250, 250, 250, 1.0f);
    [self addNavigationWithTitle:nil leftItem:nil rightItem:nil titleView:self.segment];
    [self.view addSubview:self.scrollView];
}

- (void)segmentChanged:(UISegmentedControl *)segment
{
    [_scrollView setContentOffset:CGPointMake(self.view.frame.size.width * segment.selectedSegmentIndex, 0) animated:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // view自动提升
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        [self setEdgesForExtendedLayout:UIRectEdgeNone];
    }
    [self setupUI];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    _currentIndex = scrollView.contentOffset.x / scrollView.frame.size.width;
    [self.segment setSelectedSegmentIndex:_currentIndex];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
